#!/usr/bin/env python
# -*- coding: utf-8 -*-

# @Time     :2020/11/03
# @Author   :Changshu
# @File     :Exercise_404.py
# 404,计算给定二叉树的所有左叶子之和。

class TreeNode:
	def __init__(self,x):
		self.val=x
		self.left=None
		self.right=None


def sumOfLeftLeaves(root: TreeNode) -> int:
	if root is None:
		return 0
	if root.left and root.left.left == None and root.left.right == None:
		return root.left.val + sumOfLeftLeaves(root.right)
	else:
		return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right)

if __name__ == '__main__':
	root=TreeNode(3)
	node2_1=TreeNode(9)
	node2_2 = TreeNode(20)
	node3_1 = TreeNode(15)
	node3_2 = TreeNode(7)

	root.left=node2_1
	root.right=node2_2
	node2_2.left=node3_1
	node2_2.right=node3_2

	print(sumOfLeftLeaves(root))